package com.qf.server;

import java.net.Socket;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SessionManager {
	private static Map<String, ClientData> users =new ConcurrentHashMap<String, ClientData>();
	
	//用户登录
	public static void login(String address,ClientData client) {
		
		users.put(address, client);
		
		
	}
	
	//判断用户是否已经登录
	public static boolean checkLogin(String address) {
		return users.containsKey(address);
	}
	
	//根据远程客户端的唯一地址获取对应的用户名
	public static String getUsername(String address) {
		return users.get(address).getUsername();
	}
	
	//根据提供的用户名获取该用户对应的socket对象
	public static Socket getSocketByUsername(String username) {
		
		Collection<ClientData> clients = users.values();
		
		Socket target =null;
		
		for (ClientData client : clients) {
			
			if(client.getUsername().equals(username)) {
				target=client.getSocket();
				break;
			}
			
		}
		
		return target;
		
		
		
	}
	
	
	
	
}
